package com.ansion.basedb.notify;

import androidx.annotation.NonNull;

import com.ansion.basetool.observer.ListenerManager;
import com.ansion.basetool.preconditions.Preconditions;
import com.ansion.log.VLog;
import com.ansion.user.listener.BaseContactChangeListener;


public class UserChangeNotifier extends ListenerManager<BaseContactChangeListener> {
    private final String TAG = "TAG_USER_TABLE";
    private static final UserChangeNotifier CONTACT_CHANGE_NOTIFIER = new UserChangeNotifier();

    public UserChangeNotifier() {
        super(true);
    }

    public static UserChangeNotifier getInstance() {
        return CONTACT_CHANGE_NOTIFIER;
    }


    public void notifyNameChange(String uid) {
        Preconditions.checkNotEmpty(uid);
        notifyListeners(listener -> {
            if (listener.match(uid)) {
                listener.onNameChanged(uid);
            }
        });
        if (VLog.OPEN_LOG) {
            VLog.d(TAG, "notifyNameChange,uid:%s,size:%d", uid, getListenerSize());
        }
    }

    public void notifyAvatarChange(String key, String url) {
        if (VLog.OPEN_LOG) {
            VLog.d(TAG, "notifyAvatarChange,uid:%s,url:%s,size:%d", key, url, getListenerSize());
        }
        Preconditions.checkNotEmpty(key);
        notifyListeners(listener -> {
            if (listener.match(key)) {
                listener.onFaceChanged(key, url);
            }
        });
    }

    public void notifyVipChange(String uid, boolean isVip) {
        if (VLog.OPEN_LOG) {
            VLog.d(TAG, "notifyVipChange,uid:%s,isVip:%b,size:%d", uid, isVip, getListenerSize());
        }
        Preconditions.checkNotEmpty(uid);
        notifyListeners(listener -> {
            if (listener.match(uid)) {
                listener.onVipChanged(uid, isVip);
            }
        });
    }

    public void notifyWalletChange(String uid, boolean isWallet) {
        if (VLog.OPEN_LOG) {
            VLog.d(TAG, "notifyWalletChange,uid:%s,isWallet:%b,size:%d", uid, isWallet, getListenerSize());
        }
        Preconditions.checkNotEmpty(uid);
        notifyListeners(listener -> {
            if (listener.match(uid)) {
                listener.onWalletChanged(uid, isWallet);
            }
        });
    }

    public void notifyOAVerifyChange(String uid, String iconUrl) {
        if (VLog.OPEN_LOG) {
            VLog.d(TAG, "notifyOAVerifyChange,uid:%s,iconUrl:%s", uid, iconUrl);
        }
        Preconditions.checkNotEmpty(uid);
        notifyListeners(listener -> {
            if (listener.match(uid)) {
                listener.onOaVerifiedChange(uid, iconUrl);
            }
        });
    }

    public void notifyBlockChange(@NonNull String userId, boolean block) {
        if (VLog.OPEN_LOG) {
            VLog.d(TAG, "notifyBlockChange,uid:%s,block:%b", userId, block);
        }
        Preconditions.checkNotEmpty(userId);
        notifyListeners(listener -> {
            if (listener.match(userId)) {
                listener.onBlockChanged(userId, block);
            }
        });
    }

    public void notifyBlockByChange(@NonNull String userId, boolean block) {
        Preconditions.checkNotEmpty(userId);
        notifyListeners(listener -> {
            if (listener.match(userId)) {
                listener.onBlockByOtherChanged(userId, block);
            }
        });
    }

    public void notifyDeleteAccount(@NonNull String uid) {
        notifyListeners(listener -> {
            if (listener.match(uid)) {
                listener.onDeleteAccount(uid);
            }
        });
    }

    public void notifyContactAdd(String uid) {
        if (VLog.OPEN_LOG) {
            VLog.d(TAG, "notifyContactAdd,uid:%s", uid);
        }
        notifyListeners(listener -> {
            if (listener.match(uid)) {
                listener.onContactAdd(uid);
            }
        });
    }

    public void notifyContactDelete(String uid) {
        if (VLog.OPEN_LOG) {
            VLog.d(TAG, "notifyContactDelete,uid:%s", uid);
        }
        notifyListeners(listener -> {
            if (listener.match(uid)) {
                listener.onContactDelete(uid, true);
            }
        });
    }

    public void notifyGroupMemberChange(String groupUid) {
        if (VLog.OPEN_LOG) {
            VLog.d(TAG, "notifyGroupMemberChange,groupUid:%s", groupUid);
        }
        notifyListeners(listener -> {
            if (listener.match(groupUid)) {
                listener.onGroupMemberChange(groupUid);
            }
        });
    }

    public void notifyGroupAdminChange(String groupUid, String userId, boolean isAdmin) {
        if (VLog.OPEN_LOG) {
            VLog.d(TAG, "notifyGroupAdminChange,groupUid:%s, userId:%s,isAdmin:%b", groupUid, userId, isAdmin);
        }
        notifyListeners(listener -> {
            if (listener.match(groupUid)) {
                listener.onGroupAdminChange(groupUid, userId, isAdmin);
            }
        });
    }

    public void notifyGroupDescChange(String groupId) {
        if (VLog.OPEN_LOG) {
            VLog.d(TAG, "notifyGroupDescChange,groupUid:%s", groupId);
        }
        notifyListeners(listener -> {
            if (listener.match(groupId)) {
                listener.onGroupDescChange(groupId);
            }
        });
    }

    public void notifyWallpaperChange(String uid, String wallpaper) {
        if (VLog.OPEN_LOG) {
            VLog.d(TAG, "notifyWallpaperChange,userId:%s,wallpaper:%s", uid, wallpaper);
        }
        notifyListeners(listener -> {
            if (listener.match(uid)) {
                listener.onWallpaperChanged(wallpaper);
            }
        });
    }
}
